import request from '@/utils/request'
import { createDownload } from '@/utils'

/**
 * 终止
 * @param {*} id
 * @returns
 */
export function termination(id) {
  return request({
    url: `/api/v1/ctmCustomerChange/${id}`,
    method: 'POST'
  })
}

/**
 * 审核
 * @param {*} data
 * @returns
 */
export function aduit(data) {
  return request({
    url: '/api/v1/ctmCustomerChange/review/infoCreate',
    method: 'POST',
    data
  })
}
/**
 * 根据客户编码获取收货地址
 * @param {*} customerCode
 * @returns
 */
export function customerAddress(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerAddress/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}
/**
 * 根据客户编码获取门店
 * @param {*} customerCode
 * @returns
 */
export function shopListByCustomer(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerShopBusiness/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}
/**
 * 根据客户编码获取建设门店
 * @param {*} customerCode
 * @returns
 */
export function constructShopListByCustomer(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerShopConstruct/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}
/**
 * 根据客户编码获取业务账号及送达方
 * @param {*} customerCode
 * @returns
 */
export function accountByCustomer(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerAccountTo/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}
/**
 * 查询关联轻居
 * @param {*} customerCode
 * @returns
 */
export function decorateEnterpriseByCustomer(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerDecorateEnterprise/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}
/**
 * 下载附件模板
 * @param {*} id
 * @returns
 */
export function downTemplate(id) {
  return request({
    url: `/api/v1/shopAttachment/query/${id}`,
    method: 'GET'
  }).then(res => {
    const data = res.data || []
    data.forEach(e => {
      return request({
        url: `/api/v1/file/downloadFile?fileName=${e.attachmentUrl}`,
        method: 'POST',
        responseType: 'arraybuffer'
      }).then(res => {
        createDownload(res.data, e.attachmentName, false)
      })
    })
  })
}
/**
 * 获取模板配置
 * @param {*} typeCode
 * @returns
 */
export function getTemplateConfig(typeCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = [
    { id: 'typeCode', property: 'typeCode', value: typeCode, operator: '=' }
  ]
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/api/v1/configAttachmentTemplate/page',
    method: 'POST',
    data
  }).then(res => {
    if (res.data.records.length === 0) {
      return Promise.reject(res)
    }
    return res.data.records[0]
  })
}
/**
 * 客户联系人
 * @param {*} customerCode
 * @returns
 */
export function contactByCustomer(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerContact/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}

/**
 * 门店联系人
 * @param {*} shopCode
 * @returns
 */
export function contactByShop(shopCode) {
  return request({
    url: `/api/v1/ctmCustomerContact/queryByShopCode/${shopCode}`,
    method: 'GET'
  })
}
/**
 * 获取冻结范围
 * @param {*} customerCode
 * @returns
 */
export function queryByCustomerCode(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerFreezeRange/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}

/**
 * 下载模板
 * @param {*} templateCode
 * @returns
 */
export function freezeAndThawTemplate(templateCode, filename) {
  return request({
    url: `/api/v1/excel/template/download`,
    method: 'GET',
    responseType: 'blob',
    params: {
      templateCode
    }
  }).then(res => {
    const a = document.createElement('a')
    const tmp = URL.createObjectURL(res)
    a.href = tmp
    a.download = filename
    a.click()
    setTimeout(() => {
      URL.revokeObjectURL(tmp)
    }, 30 * 1000)
    return res
  })
}
/**
 * 根据客户编码获取合伙人信息
 * @param {*} customerCode
 * @returns
 */
export function ctmCustomerPartner(customerCode) {
  return request({
    url: `/api/v1/ctmCustomerPartner/queryByCustomerCode/${customerCode}`,
    method: 'GET'
  })
}
